﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using Newtonsoft.Json;
using System.Runtime.InteropServices;
using System.Configuration;

namespace MechanicsLab
{
    public class XinGaoPress
    {
        string connStr = "";

        public XinGaoPress(string path)
        {
            //connStr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";Persist Security Info=False;";
            connStr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";Jet OLEDB:Database Password=c;";
        }

        public string GetCur(byte[] buffer)
        {
            //byte[] buffer = dt.Rows[k]["Binary"] as byte[];     //传进的数据第一条
            int size = 8;
            string outstring = "";
            byte[] strbuffer = new byte[8];
            int jk = 0;
            for (int i = 1; i <= buffer.Length; i++)
            {
                //framework2.0没有这个方法
                //byte[] strbuffer = buffer.Skip(i).Take(8).ToArray();   
                strbuffer[jk++] = buffer[i - 1];
                if ((i != 0) && (i % 8 == 0))
                {
                    //从进程的非托管堆中分配内存给structPtr
                    IntPtr strPtr = Marshal.AllocHGlobal(size);
                    //将数据从一维托管数组bytes复制到非托管内存指针strPtr    
                    Marshal.Copy(strbuffer, 0, strPtr, size);
                    //将数据从非托管内存块封送到新分配的指定类型的托管对象    
                    //将内存空间转换为目标结构体    
                    Cur obj = (Cur)Marshal.PtrToStructure(strPtr, typeof(Cur));
                    outstring += obj.Time.ToString() + "," + (Convert.ToDouble(obj.Load.ToString()) / 1000).ToString() + ",";
                    //释放以前使用 AllocHGlobal 从进程的非托管内存中分配的内存    
                    Marshal.FreeHGlobal(strPtr);
                    jk = 0;
                    strbuffer = new byte[8];
                }
            }
            return outstring;
        }

        #region 压力抗折抗压
        public string PostData(string precativeNum, string name, string remark_1)
        {
            List<PostDataPress> postdata = new List<PostDataPress>();
            if (name.Equals("水泥物理力学性能试验"))
            {
                #region 抗折数据
                DataTable dr = SelPostInfo1(precativeNum);
                if (dr != null)
                {
                    if (dr.Rows.Count > 0)
                    {
                        for (int b = 0; b < dr.Rows.Count / 3; b++)
                        {
                            PostDataPress chp = new PostDataPress();
                            chp.trialName = name;
                            chp.equNum = ConfigurationManager.AppSettings["Number"];
                            //一组数据ID
                            chp.ID = remark_1;
                            //试验单号
                            chp.Number = precativeNum;
                            //力值

                            chp.Force1 = (Convert.ToDouble(dr.Rows[(b * 3)]["OnekN"]) / 1000).ToString();
                            byte[] buffer = dr.Rows[(b * 3)]["Binary"] as byte[];
                            string[] tt = GetCur(buffer).Split(',');
                            for (int i = 0; i < tt.Length / 2; i++)
                            {
                                chp.Curve1.Add(tt[0 + (i * 2)] + "," + tt[1 + (i * 2)]);
                            }

                            chp.Force2 = (Convert.ToDouble(dr.Rows[1 + (b * 3)]["OnekN"]) / 1000).ToString();
                            byte[] buffer1 = dr.Rows[1 + (b * 3)]["Binary"] as byte[];
                            string[] tt1 = GetCur(buffer1).Split(',');
                            for (int i = 0; i < tt1.Length / 2; i++)
                            {
                                chp.Curve2.Add(tt1[0 + (i * 2)] + "," + tt1[1 + (i * 2)]);
                            }

                            chp.Force3 = (Convert.ToDouble(dr.Rows[2 + (b * 3)]["OnekN"]) / 1000).ToString();
                            byte[] buffer2 = dr.Rows[2 + (b * 3)]["Binary"] as byte[];
                            string[] tt2 = GetCur(buffer2).Split(',');
                            for (int i = 0; i < tt2.Length / 2; i++)
                            {
                                chp.Curve3.Add(tt2[0 + (i * 2)] + "," + tt2[1 + (i * 2)]);
                            }
                            postdata.Add(chp);
                        }
                    }
                }
                #endregion

                #region 抗压数据
                DataTable dv = SelPostInfo(precativeNum);
                if (dv != null)
                {
                    if (dv.Rows.Count > 0)
                    {
                        for (int b = 0; b < dv.Rows.Count / 6; b++)
                        {
                            if (postdata.Count >= dv.Rows.Count / 6)
                            {
                                byte[] buffer = null;
                                string[] tt = null;
                                //力值
                                postdata[0].Force4 = (Convert.ToDouble(dv.Rows[(b * 6)]["OnekN"]) / 1000).ToString();
                                buffer = dv.Rows[(b * 6)]["Binary"] as byte[];
                                tt = GetCur(buffer).Split(',');
                                for (int i = 0; i < tt.Length / 2; i++)
                                {
                                    postdata[0].Curve4.Add(tt[0 + (i * 2)] + "," + tt[1 + (i * 2)]);
                                }

                                postdata[0].Force5 = (Convert.ToDouble(dv.Rows[1 + (b * 6)]["OnekN"]) / 1000).ToString();
                                buffer = dv.Rows[1 + (b * 6)]["Binary"] as byte[];
                                tt = GetCur(buffer).Split(',');
                                for (int i = 0; i < tt.Length / 2; i++)
                                {
                                    postdata[0].Curve5.Add(tt[0 + (i * 2)] + "," + tt[1 + (i * 2)]);
                                }

                                postdata[0].Force6 = (Convert.ToDouble(dv.Rows[2 + (b * 6)]["OnekN"]) / 1000).ToString();
                                buffer = dv.Rows[2 + (b * 6)]["Binary"] as byte[];
                                tt = GetCur(buffer).Split(',');
                                for (int i = 0; i < tt.Length / 2; i++)
                                {
                                    postdata[0].Curve6.Add(tt[0 + (i * 2)] + "," + tt[1 + (i * 2)]);
                                }

                                postdata[0].Force7 = (Convert.ToDouble(dv.Rows[3 + (b * 6)]["OnekN"]) / 1000).ToString();
                                buffer = dv.Rows[3 + (b * 6)]["Binary"] as byte[];
                                tt = GetCur(buffer).Split(',');
                                for (int i = 0; i < tt.Length / 2; i++)
                                {
                                    postdata[0].Curve7.Add(tt[0 + (i * 2)] + "," + tt[1 + (i * 2)]);
                                }

                                postdata[0].Force8 = (Convert.ToDouble(dv.Rows[4 + (b * 6)]["OnekN"]) / 1000).ToString();
                                buffer = dv.Rows[4 + (b * 6)]["Binary"] as byte[];
                                tt = GetCur(buffer).Split(',');
                                for (int i = 0; i < tt.Length / 2; i++)
                                {
                                    postdata[0].Curve8.Add(tt[0 + (i * 2)] + "," + tt[1 + (i * 2)]);
                                }

                                postdata[0].Force9 = (Convert.ToDouble(dv.Rows[5 + (b * 6)]["OnekN"]) / 1000).ToString();
                                buffer = dv.Rows[5 + (b * 6)]["Binary"] as byte[];
                                tt = GetCur(buffer).Split(',');
                                for (int i = 0; i < tt.Length / 2; i++)
                                {
                                    postdata[0].Curve9.Add(tt[0 + (i * 2)] + "," + tt[1 + (i * 2)]);
                                }
                            }
                        }
                    }
                }
                #endregion
            }
            else
            {
                #region 抗压数据
                DataTable dn = SelPostInfo(precativeNum);
                if (dn != null)
                {
                    if (dn.Rows.Count > 0)
                    {
                        for (int b = 0; b < dn.Rows.Count / 3; b++)
                        {
                            PostDataPress chp = new PostDataPress();
                            chp.trialName = name;
                            chp.equNum = ConfigurationManager.AppSettings["Number"];
                            //一组数据ID
                            chp.ID = remark_1;
                            //试验单号
                            chp.Number = precativeNum;
                            //力值

                            chp.Force1 = (Convert.ToDouble(dn.Rows[(b * 3)]["OnekN"]) / 1000).ToString();
                            byte[] buffer = dn.Rows[(b * 3)]["Binary"] as byte[];
                            string[] tt = GetCur(buffer).Split(',');
                            for (int i = 0; i < tt.Length / 2; i++)
                            {
                                chp.Curve1.Add(tt[0 + (i * 2)] + "," + tt[1 + (i * 2)]);
                            }

                            chp.Force2 = (Convert.ToDouble(dn.Rows[1 + (b * 3)]["OnekN"]) / 1000).ToString();
                            byte[] buffer1 = dn.Rows[1 + (b * 3)]["Binary"] as byte[];
                            string[] tt1 = GetCur(buffer1).Split(',');
                            for (int i = 0; i < tt1.Length / 2; i++)
                            {
                                chp.Curve2.Add(tt1[0 + (i * 2)] + "," + tt1[1 + (i * 2)]);
                            }

                            chp.Force3 = (Convert.ToDouble(dn.Rows[2 + (b * 3)]["OnekN"]) / 1000).ToString();
                            byte[] buffer2 = dn.Rows[2 + (b * 3)]["Binary"] as byte[];
                            string[] tt2 = GetCur(buffer2).Split(',');
                            for (int i = 0; i < tt2.Length / 2; i++)
                            {
                                chp.Curve3.Add(tt2[0 + (i * 2)] + "," + tt2[1 + (i * 2)]);
                            }

                            postdata.Add(chp);
                        }
                    }
                }
            }
                #endregion

            string jsonstr = JsonConvert.SerializeObject(postdata);
            string str = Pub.HttpPost(ConfigurationManager.AppSettings["PostDataUrl"], jsonstr);
            return str;
        }

        /// <summary>
        /// 抗压数据
        /// </summary>
        /// <param name="precativeNum"></param>
        /// <returns></returns>
        public DataTable SelPostInfo(string precativeNum)
        {
            DataTable dt = null;
            try
            {
                string sql = "select * from SNKYGROne where ZuHao='" + precativeNum + "'";
                dt = Query(sql);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        /// <summary>
        /// 抗折数据
        /// </summary>
        /// <param name="precativeNum"></param>
        /// <returns></returns>
        public DataTable SelPostInfo1(string precativeNum)
        {
            DataTable dt = null;
            try
            {
                string sql = "select * from SNKZOne where ZuHao='" + precativeNum + "'";
                dt = Query(sql);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }
        #endregion

        #region 水泥混凝土抗压
        public string PostDataHNT(string precativeNum, string name, string remark_1)
        {
            List<PostDataPress> postdata = new List<PostDataPress>();
            DataTable dr = GetHNTData(precativeNum);//获取数据；
            if (dr != null)
            {
                if (dr.Rows.Count > 0)
                {
                    for (int b = 0; b < dr.Rows.Count / 3; b++)
                    {
                        PostDataPress chp = new PostDataPress();
                        chp.trialName = name;
                        chp.equNum = ConfigurationManager.AppSettings["Number"];
                        //一组数据ID
                        chp.ID = remark_1;
                        //试验单号
                        chp.Number = precativeNum;
                        //力值

                        chp.Force1 = (Convert.ToDouble(dr.Rows[(b * 3)]["OnekN"]) / 1000).ToString();
                        
                        byte[] buffer = dr.Rows[(b * 3)]["Binary"] as byte[];
                        string[] tt = GetCur(buffer).Split(',');
                        for (int i = 0; i < tt.Length / 2; i++)
                        {
                            chp.Curve1.Add(tt[0 + (i * 2)] + "," + tt[1 + (i * 2)]);
                        }

                        chp.Force2 = (Convert.ToDouble(dr.Rows[1 + (b * 3)]["OnekN"]) / 1000).ToString();
                        byte[] buffer1 = dr.Rows[1 + (b * 3)]["Binary"] as byte[];
                        string[] tt1 = GetCur(buffer1).Split(',');
                        for (int i = 0; i < tt1.Length / 2; i++)
                        {
                            chp.Curve2.Add(tt1[0 + (i * 2)] + "," + tt1[1 + (i * 2)]);
                        }

                        chp.Force3 = (Convert.ToDouble(dr.Rows[2 + (b * 3)]["OnekN"]) / 1000).ToString();
                        byte[] buffer2 = dr.Rows[2 + (b * 3)]["Binary"] as byte[];
                        string[] tt2 = GetCur(buffer2).Split(',');
                        for (int i = 0; i < tt2.Length / 2; i++)
                        {
                            chp.Curve3.Add(tt2[0 + (i * 2)] + "," + tt2[1 + (i * 2)]);
                        }
                        postdata.Add(chp);
                    }
                }
            }

            return "";
        }

        /// <summary>
        /// 获得试验单号的试验数据
        /// </summary>
        /// <param name="precativeNum">需要获取的试验单号</param>
        /// <returns></returns>
        public DataTable GetHNTData(string precativeNum)
        {
            DataTable dt = null;
            try
            {
                string sql = string.Format("select * from HNTKYOne where ZuHao='{0}'", precativeNum);
                dt = Query(sql);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }
        #endregion

        public DataTable Query(string sql)
        {
            OleDbConnection conn = null;
            try
            {
                DataTable table = new DataTable();
                conn = new OleDbConnection(connStr);
                conn.Open();
                using (OleDbDataAdapter da = new OleDbDataAdapter(sql, conn))
                {
                    da.Fill(table);
                }
                return table;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                conn.Close();
            }
        }
    }
}
